package com.sky.task;


import com.sky.entity.Orders;
import com.sky.mapper.OrderMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.util.List;

@Component
@Slf4j
public class OrderTask {

    @Autowired
    private OrderMapper orderMapper;

    @Scheduled(cron = "0 * * * * ?")
    public void processTimeOut(){
        log.info("超时订单的处理");
        //得到当前时间
        LocalDateTime localDateTime = LocalDateTime.now().plusMinutes(15);
        // 查出超时订单数据
        List<Orders> ordersList = orderMapper.getByOrderTimeAndStatus(Orders.PENDING_PAYMENT, localDateTime);

        if(ordersList != null && ordersList.size() > 0){
            ordersList.forEach(order -> {
                order.setStatus(Orders.CANCELLED);
                order.setCancelReason("支付超时, 自动取消");
                order.setOrderTime(LocalDateTime.now());
                orderMapper.update(order);
            });
        }
    }

    @Scheduled(cron = "0 0 1 * * ?")
//    @Scheduled(cron = "0/5 * * * * ?")
    public void processDelivey(){
        log.info("处理派送中的订单: {}", LocalDateTime.now());
    }
}
